package accs.gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LinearGradientPaint;
import java.awt.Point;
import java.awt.RenderingHints;

import javax.swing.JPanel;

public class ProgressPanel extends JPanel {
	private int working=0;
	private int notWorking=0;
	private int total=1;
	
	public ProgressPanel() {
	}
	
	public void setParts(int working, int notWorking, int total) {
		this.working=working;
		this.notWorking=notWorking;
		this.total=total;
		repaint();
	}
	
	public void paintComponent(Graphics g) {
		Graphics2D g2d=(Graphics2D)g;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		
		g.setColor(getBackground());
		g.fillRect(0,0,getWidth(),getHeight());
		
		if(total>0) {
			int workingPixel=working*getWidth()/total;
			int notWorkingPixel=notWorking*getWidth()/total;
			
			paintRect(g2d, 0, workingPixel, new Color(0,255,0), new Color(200,255,200), working);
			paintRect(g2d, workingPixel, notWorkingPixel, new Color(255,0,0), new Color(255,200,200), notWorking);
			paintRect(g2d, workingPixel+notWorkingPixel, getWidth()-workingPixel-notWorkingPixel, Color.LIGHT_GRAY, Color.WHITE, total-working-notWorking);
			
			int radius=Math.min(getWidth(),getHeight())/4;
			g.setColor(Color.DARK_GRAY);
			g.drawRoundRect(0,0,getWidth()-1,getHeight()-1,radius,radius);
		}
	}
	
	private void paintRect(Graphics2D g, int startX, int width, Color dark, Color bright, int number) {
		int radius=Math.min(getWidth(),getHeight())/4;
		
		g.setPaint(new LinearGradientPaint(new Point(0,0),new Point(0,getHeight()),new float[]{0,0.7f,1},new Color[]{bright,dark,bright}));
		g.fillRoundRect(startX,0,width,getHeight(),radius,radius);
		
		g.setColor(Color.BLACK);
		
		g.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,getHeight()/2));
		int height=g.getFontMetrics().getHeight();
		int fontWidth=g.getFontMetrics().stringWidth(String.valueOf(number));
		if(fontWidth<=width) {
			g.drawString(String.valueOf(number),(width-fontWidth)/2+startX,getHeight()-height/2);
		}
	}
}